home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / CWWatcher.cp < prev    next >
Text File  |  1997-06-28  |  7KB  |  351 lines

  1. // CWWatcher.cp
  2.  
  3. #ifndef CWWatcher_h
  4. #include "CWWatcher.h"
  5. #endif
  6. #ifndef ProcessLoop_h
  7. #include "ProcessLoop.h"
  8. #endif
  9. #ifndef ProcessInfo_h
  10. #include "ProcessInfo.h"
  11. #endif
  12. #ifndef WindowObject_h
  13. #include "WindowObject.h"
  14. #endif
  15. #ifndef OSError_h
  16. #include "OSError.h"
  17. #endif
  18. #ifndef BufferOfSize_h
  19. #include "BufferOfSize.h"
  20. #endif
  21. #ifndef CompileStatistics_h
  22. #include "CompileStatistics.h"
  23. #endif
  24.  
  25. CompileStatistics CWWatcher::stats;
  26.  
  27. CWWatcher::CWWatcher()
  28.   : QDPatchBlock( const_cast<GrafPortObject&>( ToolBar()->Port() ) ),
  29.      fileLine( 0 ),
  30.      oldLines( 0 ),
  31.      numberToIgnore( maxuint32 ),
  32.      totalLines( 0 ),
  33.      filesLeft( 0 )
  34.   {
  35.     ActivateTextPatch();
  36.     Count();
  37.   }
  38.  
  39. CWWatcher::~CWWatcher()
  40.   {
  41.     if ( stats.Known( file ) )
  42.       {
  43.         uint32 oldFileLines = stats.FileLines( file );
  44.         if ( oldFileLines > fileLine )
  45.            {
  46.             totalLines -= oldFileLines - fileLine;
  47.             stats.SetFileLines( file, fileLine );
  48.           }
  49.       }
  50.   }
  51.  
  52. void CWWatcher::DrawText( ConstData text,
  53.                                   Point numerator,
  54.                                   Point denominator )
  55.   {
  56.     QDPatchBlock::DrawText( text, numerator, denominator );
  57.  
  58.     uint32 lineStart;
  59.     for ( lineStart = 0; lineStart + 6 < text.Length(); lineStart++ )
  60.         if ( text[lineStart] == 'L'
  61.               && text[lineStart + 1] == 'i'
  62.               && text[lineStart + 2] == 'n'
  63.               && text[lineStart + 3] == 'e'
  64.               && text[lineStart + 4] == ':' )
  65.             break;
  66.     
  67.     if ( lineStart + 6 < text.Length() )
  68.       {
  69.         String255 numeral( text.Tail( lineStart + 6 ) );
  70.         StringToNum( numeral, &fileLine );
  71.         
  72.         if ( fileLine == numberToIgnore )
  73.             fileLine = 0;
  74.          else
  75.             numberToIgnore = maxuint32;
  76.         
  77.         if ( stats.Known( file ) )
  78.           {
  79.             uint32 oldFileLines = stats.FileLines( file );
  80.             if ( oldFileLines < fileLine )
  81.                {
  82.                 totalLines += fileLine - oldFileLines;
  83.                 stats.SetFileLines( file, fileLine );
  84.               }
  85.           }
  86.       }
  87.      else if ( text.StartsWith( ConstPString( "\pCompiling: " ) ) )
  88.       {
  89.         String255 newFile( text.Middle( URange32( 12, text.Length() - 1 ) ) );
  90.         if ( ConstPString(newFile) == ConstPString(file) )
  91.             return;
  92.  
  93.         if ( stats.Known( file ) )
  94.           {
  95.             uint32 oldFileLines = stats.FileLines( file );
  96.             if ( oldFileLines > fileLine )
  97.                {
  98.                 totalLines -= oldFileLines - fileLine;
  99.                 stats.SetFileLines( file, fileLine );
  100.               }
  101.           }
  102.         
  103.         oldLines += fileLine;
  104.         numberToIgnore = fileLine;
  105.         fileLine = 0;
  106.         
  107.         if ( file.Length() != 0 )
  108.             filesLeft--;
  109.         
  110.          file = newFile;
  111.       }
  112.     
  113.     Announce();
  114.   }
  115.  
  116. ConstData CWWatcher::Partition()
  117.   {
  118.     for ( ProcessLoop process; process.Unfinished(); process++ )
  119.       {
  120.         ProcessInfo info( *process );
  121.         if ( info.Signature() == FileSignature::Make( 'CWIE' ) )
  122.             return info.Partition();
  123.       }
  124.     
  125.     return ConstData();
  126.   }
  127.  
  128. const WindowObject *CWWatcher::ToolBar()
  129.   {
  130.     ConstData partition( Partition() );
  131.     
  132.     for ( uint32 offset = 0; offset < partition.Length(); offset+=4 )
  133.       {
  134.         const WindowObject *w = reinterpret_cast<const WindowObject *>( &partition[offset] );
  135.         
  136.         if ( w->windowKind == 0x4e20
  137.               && w->port.portRect.right == 465
  138.               && w->port.portRect.bottom == 17
  139.               && w->port.portRect.left == 0
  140.               && w->port.portRect.top == 0
  141.               && w->titleWidth == 0
  142.               && w->controlList == 0
  143.               && w->windowPic == 0 )
  144.             return w;
  145.       }
  146.     
  147.     return 0;
  148.   }
  149.  
  150. void CWWatcher::Count()
  151.   {
  152.     OSType signature = 'CWIE';
  153.     
  154.     AEAddressDesc address;
  155.     OSError error = AECreateDesc( typeApplSignature,
  156.                                             &signature,
  157.                                             sizeof( signature ),
  158.                                             &address );
  159.     error.Debug();
  160.     error.Throw();
  161.     
  162.     AppleEvent event;
  163.     error = AECreateAppleEvent( 'MMPR',
  164.                                         'GSeg',
  165.                                         &address,
  166.                                         kAutoGenerateReturnID,
  167.                                         kAnyTransactionID,
  168.                                         &event );
  169.     error.Debug();
  170.     error.Throw();
  171.                                                    
  172.     AppleEvent reply;
  173.     error = AESend( &event,
  174.                         &reply,
  175.                         kAEWaitReply,
  176.                         kAENeverInteract,
  177.                         5 * 60 * 60,
  178.                         0,
  179.                         0 );
  180.     error.Debug();
  181.     error.Throw();
  182.     
  183.     AEDescList segmentList;
  184.     error = AEGetParamDesc( &reply, keyDirectObject, typeAEList, &segmentList );
  185.     error.Debug();
  186.     error.Throw();
  187.     
  188.     error = AEDisposeDesc( &reply );
  189.     error.Debug();
  190.     error.Throw();
  191.     
  192.     int32 segmentCount;
  193.     error = AECountItems( &segmentList, &segmentCount );    
  194.     error.Debug();
  195.     error.Throw();
  196.     
  197.     filesLeft = 0;
  198.     
  199.     for ( uint16 segmentNumber = 1; segmentNumber <= segmentCount; segmentNumber++ )
  200.       {
  201.         AERecord segmentRecord;
  202.         OSType key;
  203.         error = AEGetNthDesc( &segmentList,
  204.                                      segmentNumber,
  205.                                      typeAERecord,
  206.                                      &key,
  207.                                      &segmentRecord );
  208.         error.Debug();
  209.         error.Throw();
  210.     
  211.         uint16 fileCount;
  212.         int32 actualSize;
  213.         OSType type;
  214.  
  215.         error = AEGetKeyPtr( &segmentRecord,
  216.                                     'NumF',
  217.                                     typeSMInt,
  218.                                     &type,
  219.                                     &fileCount,
  220.                                     sizeof( fileCount ),
  221.                                     &actualSize );
  222.  
  223.         error.Debug();
  224.         error.Throw();
  225.         Assert( actualSize == sizeof( fileCount ) );
  226.         
  227.         for ( uint16 fileNumber = 1; fileNumber <= fileCount; fileNumber++ )
  228.           {
  229.             AppleEvent fileEvent;
  230.             error = AECreateAppleEvent( 'MMPR',
  231.                                                 'GFil',
  232.                                                 &address,
  233.                                                 kAutoGenerateReturnID,
  234.                                                 kAnyTransactionID,
  235.                                                 &fileEvent );
  236.             error.Debug();
  237.             error.Throw();
  238.             
  239.             error = AEPutParamPtr( &fileEvent,
  240.                                           keyDirectObject,
  241.                                           typeSMInt,
  242.                                           &fileNumber,
  243.                                           sizeof( fileNumber ) );
  244.             error.Debug();
  245.             error.Throw();
  246.             
  247.             error = AEPutParamPtr( &fileEvent,
  248.                                           'Segm',
  249.                                           typeSMInt,
  250.                                           &segmentNumber,
  251.                                           sizeof( segmentNumber ) );
  252.             error.Debug();
  253.             error.Throw();
  254.              
  255.             AppleEvent fileReply;
  256.             error = AESend( &fileEvent,
  257.                                 &fileReply,
  258.                                 kAEWaitReply,
  259.                                 kAENeverInteract,
  260.                                 5 * 60 * 60,
  261.                                 0,
  262.                                 0 );
  263.             error.Debug();
  264.             error.Throw();
  265.             
  266.             AERecord fileRecord;
  267.             error = AEGetParamDesc( &fileReply,
  268.                                             keyDirectObject,
  269.                                             typeAERecord,
  270.                                             &fileRecord );
  271.  
  272.             bool found = error.OK();
  273.             error.Fix( errAECoercionFail );
  274.  
  275.             error.Debug();
  276.             error.Throw();
  277.             
  278.             error = AEDisposeDesc( &fileReply );
  279.             error.Debug();
  280.             error.Throw();
  281.             
  282.             if ( !found )
  283.                 continue;
  284.             
  285.             int32 upToDate = 0;
  286.             int32 actualSize;
  287.             OSType type;
  288.  
  289.             error = AEGetKeyPtr( &fileRecord,
  290.                                         'UpTD',
  291.                                         typeBoolean,
  292.                                         &type,
  293.                                         &upToDate,
  294.                                         sizeof( upToDate ),
  295.                                         &actualSize );
  296.             
  297.             error.Debug();
  298.             error.Throw();
  299.             Assert( actualSize <= sizeof( upToDate ) );
  300.         
  301.             if ( upToDate == 0 )
  302.               {
  303.                 static BufferOfSize<64> name;
  304.                 name.Reset();
  305.  
  306.                 error = AEGetKeyPtr( &fileRecord,
  307.                                             'pnam',
  308.                                             typeChar,
  309.                                             &type,
  310.                                             name.Unused().Start(),
  311.                                             name.Unused().Length(),
  312.                                             &actualSize );
  313.                 error.Debug();
  314.                 error.Throw();
  315.  
  316.                 name.AdvanceMark( actualSize );
  317.                 
  318.                 String255 string( name.Used() );
  319.                 SeeUncompiledFile( string );
  320.               }
  321.             
  322.             error = AEDisposeDesc( &fileRecord );
  323.             error.Debug();
  324.             error.Throw();
  325.           }
  326.         
  327.         error = AEDisposeDesc( &segmentRecord );
  328.         error.Debug();
  329.         error.Throw();
  330.       }
  331.         
  332.     error = AEDisposeDesc( &segmentList );
  333.     error.Debug();
  334.     error.Throw();
  335.         
  336.     error = AEDisposeDesc( &address );
  337.     error.Debug();
  338.     error.Throw();
  339.   }
  340.  
  341. void CWWatcher::SeeUncompiledFile( ConstPString name )
  342.   {
  343.     filesLeft++;
  344.     
  345.     if ( !stats.Known( name ) )
  346.         stats.AddFile( name );
  347.     
  348.     totalLines += stats.FileLines( name );
  349.   }
  350.  
  351.